package comm.suger.web.utils;

import java.io.UnsupportedEncodingException;

/**
 * @ClassName: CharsetUtils
 * @Description: 字符编码工具类
 * @author Lanxiaowei
 * @date 2014-2-23 下午4:50:26
 */
public class CharsetUtils {
	/**
	 * 转换编码 ISO-8859-1到GB2312
	 * 
	 * @param text
	 * @return
	 */
	public static final String iso2GB2312(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("ISO-8859-1"), "GB2312");
		} catch (UnsupportedEncodingException ex) {
			result = ex.toString();
		}
		return result;
	}

	/**
	 * 转换编码 GB2312到ISO-8859-1
	 * 
	 * @param text
	 * @return
	 */
	public static final String gb2ISO(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("GB2312"), "ISO-8859-1");
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 转换编码 ISO-8859-1到UTF-8
	 * 
	 * @param text
	 * @return
	 */
	public static final String iso2UTF8(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException ex) {
			result = ex.toString();
		}
		return result;
	}
	
	/**
	 * 转换编码 UTF8到ISO-8859-1
	 * 
	 * @param text
	 * @return
	 */
	public static final String utf82ISO(String text) {
		String result = "";
		try {
			result = new String(text.getBytes("UTF-8"), "ISO-8859-1");
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		return result;
	}
}
